package com.ddzh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddzh.mapper.MenuMapper;
import com.ddzh.pojo.Menu;
import com.ddzh.service.MenuService;
import com.ddzh.vo.MenuVo;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;

import java.util.*;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    public List<MenuVo> getUserMenuTrees(){
        String currentUser = RpcContext.getContext().getAttachment("currentUser");
        System.out.println("service方法获取到用户名："+currentUser);
        List<MenuVo> list = this.baseMapper.getUserMenuTrees(currentUser);
        Map<Long,MenuVo> temp = new HashMap<Long, MenuVo>();
        if(list != null && list.isEmpty() == false){
            for(MenuVo treeNode : list){
                if (treeNode!=null) {
                    temp.put(treeNode.getId(), treeNode);
                }
            }
            list.clear();
            for(MenuVo treeNode : temp.values()){
                if(treeNode.getPid() == 0){
                    list.add(treeNode);
                }else{
                    MenuVo parent = temp.get(treeNode.getPid());
                    if(parent.getChildren() == null){
                        parent.setChildren(new ArrayList<MenuVo>());
                    }
                    parent.getChildren().add(treeNode);
                }
            }
        }
        for (MenuVo menuVo : list) {
            System.out.println("menuVo.toString() = " + menuVo.toString());
        }
        return list;
    }


    @Override
    public  List<MenuVo> getMenuTree() {
        //查询所有数据
        List<MenuVo> list = this.baseMapper.findAllMenu();
        System.out.println("list = " + list);
        //判null
        if (list!=null && list.isEmpty()==false) {
            //遍历list集合，保存在Map集合中
            HashMap<Long, MenuVo> temp = new HashMap<>();
            System.out.println("temp = " + temp);

            list.forEach(menu -> {
                temp.put(menu.getId(), menu);
            });
            //清空list集合数据，在list存放整个属性返回的第一层结构
            list.clear();
            //组装树形结构,需要遍历map集合中每一个元素
            for (MenuVo menuVo : temp.values()) {
                Long pid = menuVo.getPid();
                MenuVo parent = temp.get(pid);
                if (parent==null){
                    list.add(menuVo);
                }else{
                    //这个思想用到了很多次：修改和添加数据一起写，添加时：先创建节点，为节点设置值修改和添加是一样的，统一处理
                    //孩子节点找到了父节点,当前节点存入父节点
                    if (parent.getChildren()==null){
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(menuVo);
                }
            }
            return list;
        }
        return null;
    }

    @Override
    public List<String> findMenuPathByURoleId(Long roleId) {
        return this.baseMapper.findMenuPathByURoleId(roleId);
    }

//    public static void main(String[] args) {
//        MenuServiceImpl service = new MenuServiceImpl();
//        System.out.println("service = " + service);
//        List<MenuVo> menuTree =service.getMenuTree();
//        for (MenuVo menuVo : menuTree) {
//            System.out.println("menuVo = " + menuVo);
//        }
//    }
}
